#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== BLESDK 自动编译运行脚本 ===${NC}"

# 检查CocoaPods是否安装
if ! command -v pod &> /dev/null; then
    echo -e "${YELLOW}CocoaPods未安装，正在安装...${NC}"
    sudo gem install cocoapods
    if [ $? -ne 0 ]; then
        echo -e "${RED}CocoaPods安装失败，请手动安装${NC}"
        exit 1
    fi
else
    echo -e "${GREEN}CocoaPods已安装${NC}"
fi

# 导航到Example目录
echo -e "${GREEN}导航到Example目录...${NC}"
cd ~/Desktop/BLESDK/Example || { echo -e "${RED}无法找到Example目录${NC}"; exit 1; }

# 检查AppDelegate文件
echo -e "${YELLOW}检查AppDelegate文件...${NC}"
if [ -f "BLESDK/AppDelegate.h" ] && [ -f "BLESDK/AppDelegate.m" ]; then
    echo -e "${GREEN}AppDelegate文件存在${NC}"
else
    echo -e "${RED}AppDelegate文件缺失!${NC}"
    echo -e "${YELLOW}尝试修复...${NC}"
    
    # 确保BLESDK目录存在
    if [ ! -d "BLESDK" ]; then
        echo -e "${YELLOW}创建BLESDK目录...${NC}"
        mkdir -p BLESDK
    fi
    
    # 如果缺少AppDelegate.h，创建它
    if [ ! -f "BLESDK/AppDelegate.h" ]; then
        echo -e "${YELLOW}创建AppDelegate.h文件...${NC}"
        cat > BLESDK/AppDelegate.h << 'EOF'
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
EOF
    fi
    
    # 如果缺少AppDelegate.m，创建它
    if [ ! -f "BLESDK/AppDelegate.m" ]; then
        echo -e "${YELLOW}创建AppDelegate.m文件...${NC}"
        cat > BLESDK/AppDelegate.m << 'EOF'
#import "AppDelegate.h"
#import <BLESDK/BLESDK.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 初始化SDK
    [BLESDK initSDK];
    
    // 开启调试日志
    [BLESDK setDebugLogEnabled:YES];
    
    return YES;
}

@end
EOF
    fi
fi

# 检查main.m文件
if [ ! -f "BLESDK/main.m" ]; then
    echo -e "${YELLOW}创建main.m文件...${NC}"
    cat > BLESDK/main.m << 'EOF'
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
EOF
fi

# 添加镜像源配置
echo -e "${YELLOW}配置CocoaPods镜像源以解决网络问题...${NC}"
if [ ! -f ~/.cocoapods/repos/master/config.json ]; then
    mkdir -p ~/.cocoapods/repos/master
fi

# 修改Podfile以使用本地路径
echo -e "${YELLOW}检查Podfile配置...${NC}"
if grep -q "pod 'BLESDK', :path => '../'" Podfile; then
    echo -e "${GREEN}Podfile配置正确${NC}"
else
    echo -e "${YELLOW}修改Podfile以使用本地路径...${NC}"
    # 备份原始Podfile
    cp Podfile Podfile.backup
    # 修改Podfile
    cat > Podfile << 'EOF'
use_frameworks!

platform :ios, '9.0'

target 'BLESDK_Example' do
  pod 'BLESDK', :path => '../'

  target 'BLESDK_Tests' do
    inherit! :search_paths

    pod 'Specta'
    pod 'Expecta'
    pod 'FBSnapshotTestCase'
    pod 'Expecta+Snapshots'
  end
end
EOF
fi

# 创建本地Pods目录结构
if [ ! -d "Pods" ]; then
    echo -e "${YELLOW}创建本地Pods目录...${NC}"
    mkdir -p Pods
fi

# 尝试使用本地路径安装依赖
echo -e "${GREEN}安装CocoaPods依赖...${NC}"
echo -e "${YELLOW}正在尝试方法1: 使用本地路径...${NC}"
pod install --verbose || {
    echo -e "${YELLOW}方法1失败，尝试方法2: 使用--no-repo-update选项...${NC}"
    pod install --no-repo-update --verbose || {
        echo -e "${YELLOW}方法2失败，尝试方法3: 尝试更新spec仓库...${NC}"
        pod repo update
        pod install --verbose || {
            echo -e "${RED}所有安装方法都失败。${NC}"
            echo -e "${YELLOW}您可以尝试手动运行以下命令:${NC}"
            echo "cd ~/Desktop/BLESDK/Example"
            echo "pod install --verbose"
            exit 1
        }
    }
}

# 检查是否存在项目文件
if [ ! -d "BLESDK.xcodeproj" ]; then
    echo -e "${RED}未找到项目文件! 尝试创建基本项目结构...${NC}"
    # 这里可以添加创建基本项目文件的代码
    # 但这超出了shell脚本的简单范围，需要使用xcodebuild
fi

# 检查安装是否成功
if [ -d "Pods" ] && [ -f "BLESDK.xcworkspace/contents.xcworkspacedata" ]; then
    echo -e "${GREEN}依赖安装成功!${NC}"
    
    # 打开Xcode工作区
    echo -e "${GREEN}正在打开Xcode工作区...${NC}"
    open BLESDK.xcworkspace
    
    echo -e "${YELLOW}请在Xcode中:${NC}"
    echo -e "${YELLOW}1. 确保AppDelegate.h和AppDelegate.m文件已添加到目标成员中${NC}"
    echo -e "${YELLOW}2. 选择适当的模拟器或设备${NC}"
    echo -e "${YELLOW}3. 点击运行按钮(▶)${NC}"
    echo -e "${YELLOW}4. 如果出现'Undefined symbol: _OBJC_CLASS_$_AppDelegate'错误，请检查AppDelegate文件是否被正确添加到目标中${NC}"
    echo -e "${YELLOW}5. 如果出现签名问题，请在Xcode中配置开发者账号${NC}"
    
    # 提供手动构建命令作为参考
    echo -e "${GREEN}==========================================================${NC}"
    echo -e "${GREEN}如果您想通过命令行构建和运行，可以使用以下命令:${NC}"
    echo -e "${YELLOW}cd ~/Desktop/BLESDK/Example${NC}"
    echo -e "${YELLOW}xcodebuild -workspace BLESDK.xcworkspace -scheme BLESDK_Example -destination 'platform=iOS Simulator,name=iPhone 12' build${NC}"
    echo -e "${GREEN}==========================================================${NC}"
else
    echo -e "${RED}依赖安装可能未完全成功，请检查上述错误信息${NC}"
    exit 1
fi

# 如果要使用xcodebuild自动构建和运行，可以取消注释以下命令行
# 但需要提供正确的scheme名称和目标设备
#
# echo -e "${GREEN}开始构建和运行...${NC}"
# xcodebuild -workspace BLESDK.xcworkspace -scheme BLESDK_Example -destination 'platform=iOS Simulator,name=iPhone 12' build run 